package com.pipe.util;

import io.netty.buffer.ByteBuf;
import io.netty.util.ByteProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kiven
 */
public class MyBytebufUtil {
    private static Logger LOG = LoggerFactory.getLogger(MyBytebufUtil.class);

    public static String readStringFromBytebuf(ByteBuf byteBuf, int readLength){
        byte[] content = new byte[readLength];
        byteBuf.readBytes(content);
        return new String(content);
    }

    public static byte[] readByteFromBytebuf(ByteBuf byteBuf, int readLength){
        byte[] content = new byte[readLength];
        byteBuf.readBytes(content);
        return content;
    }

    public static void printBytebuf(ByteBuf byteBuf){
        StringBuffer sb = new StringBuffer();
        byteBuf.forEachByte(new ByteProcessor() {

            @Override
            public boolean process(byte b) throws Exception {
                sb.append(Integer.toHexString(b)).append(" ");
                return true;
            }
        });
        LOG.debug(sb.toString());
    }
}